package com.itheima.examination5;

import java.util.TreeSet;

public class StudentTestLambda {
    public static void main(String[] args) {
        // 创建TreeSet集合，使用Lambda表达式定义排序规则
        TreeSet<Student> studentSet = new TreeSet<>(
                (s1, s2) -> {
                    // 主要条件：按总分从高到低
                    int totalScoreCompare = Integer.compare(s2.getTotalScore(), s1.getTotalScore());
                    if (totalScoreCompare != 0) {
                        return totalScoreCompare;
                    }
                    // 次要条件：总分相同时按物理成绩从高到低
                    return Integer.compare(s2.getPhysic(), s1.getPhysic());
                }
        );

        // 实例化Student对象并添加到集合
        studentSet.add(new Student("爱迪生", 90, 55));
        studentSet.add(new Student("牛顿", 100, 90));
        studentSet.add(new Student("拉瓦锡", 70, 100));
        studentSet.add(new Student("霍金", 100, 100));
        studentSet.add(new Student("伽利略", 60, 85));
        studentSet.add(new Student("达尔文", 70, 75));

        // 遍历集合打印所有元素信息
        System.out.println("按总分从高到低排序（总分相同按物理成绩从高到低）：");
        studentSet.forEach(System.out::println);
    }
}
